const express = require("express")
const getchooselist = express.Router()
const mysql = require("mysql2");


getchooselist.get('/choose', (req, res) => {
    const db = mysql.createPool({
        host: '127.0.0.1',
        user: 'root',
        password: '123',
        database: 'tianhetaidb2019'
    })
    const sqlStr = 'select CqlTitle,CtIdno,CqlShowId from tbconstitutionquestionlist order by CqlShowId ASC;';
    db.query(sqlStr, (err, result) => {
        if (err) console.log(err)
        result.sort((a, b) => {
            return a.CtIdno - b.CtIdno
        })
        //拿到各个题目的数量
        let arr = []
        for (let i of result) {
            arr.push(i.CtIdno)
        }

        function getRepeatNum() {
            return arr.reduce(function (prev, next) {
                prev[next] = (prev[next] + 1) || 1;
                return prev;
            }, {});
        }

        let choosefen = getRepeatNum()

        let arr2 = []
        let ress = 0
        for (let i in choosefen){
          ress += choosefen[i]
            arr2.push(ress)
        }
        // console.log(arr2)
        // console.log(result)
        res.send({
            res: result,
            choosefen: choosefen,
            arr2
        })

    })
})

module.exports = getchooselist
